package GUILayer;

import ControlLayer.GroupCtr;

import ModelLayer.Group;
import ModelLayer.RealException;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.BoxLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;
import javax.swing.JScrollPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class GroupUI extends JPanel {

	/**
	 * 
	 */
	private GroupCtr groupCtr;

	private static final long serialVersionUID = 1L;
	private JTextField searchGroupBox;

	private JPanel groupsContainer;
	private JPanel showAllGroups;
	private JPanel showGroup;
	private JPanel createGroup;
	private JPanel editGroup;
	private JTextField createQuantityDiscountField;
	private JTextField createPickupDiscountField;
	private JTextField createPaymentTermField;
	private JTextField createCraftmanDiscountField;
	private JTable table;
	private DefaultTableModel tableModel;
	private JTextField editGroupIDField;
	private JTextField editGroupPaymentTermField;
	private JTextField editGroupCraftmanDiscountField;
	private JTextField editGroupQuantityDiscountField;
	private JTextField editGroupPickupDiscountField;
	private JTextField showIDField;
	private JTextField showPaymentTermField;
	private JTextField showCraftmanDiscountPanel;
	private JTextField showQuantitryDiscountField;
	private JTextField showPickupDiscountfield;

	public GroupUI() {
		groupCtr = new GroupCtr();
		initialize();
	}

	public void initialize() {

		String loggedInPosition = LoginUI.getLoggedInPosition();

		tableModel = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel.addColumn("ID");
		tableModel.addColumn("Payment Term");
		tableModel.addColumn("Craftman Discount");
		tableModel.addColumn("Quantity Discount");
		tableModel.addColumn("Pickup Discount");

		this.setLayout(new BorderLayout(0, 0));

		JTabbedPane subMenu = new SubMenuTabbedPane();
		this.add(subMenu, BorderLayout.CENTER);

		JPanel groupsTab = new JPanel();
		subMenu.addTab("Groups", null, groupsTab, null);
		groupsTab.setLayout(new BorderLayout(0, 0));

		JPanel groupsMenu = new JPanel();
		groupsTab.add(groupsMenu, BorderLayout.NORTH);
		groupsMenu.setBorder(new TitledBorder(new EtchedBorder(
				EtchedBorder.LOWERED, null, null), "Groups Menu",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				new Color(0, 0, 0)));
		groupsMenu.setLayout(new BorderLayout(0, 0));

		JPanel groupsMenuLeft = new JPanel();
		groupsMenu.add(groupsMenuLeft, BorderLayout.WEST);

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("manager")) {
			JButton btnCreateGroup = new JButton("Create Group");
			btnCreateGroup.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CardLayout cl = (CardLayout) groupsContainer.getLayout();
					cl.show(groupsContainer, "createGroup");
				}
			});
			groupsMenuLeft.add(btnCreateGroup);
		}
		JPanel groupsMenuRight = new JPanel();
		groupsMenu.add(groupsMenuRight, BorderLayout.EAST);

		JLabel searchGroupId = new JLabel("ID:");
		groupsMenuRight.add(searchGroupId);

		searchGroupBox = new JTextField();
		groupsMenuRight.add(searchGroupBox);
		searchGroupBox.setColumns(5);

		JButton btnSearchGroup = new JButton("Show");
		btnSearchGroup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showGroup(Integer.parseInt(searchGroupBox.getText()));
				searchGroupBox.setText("");
			}
		});
		groupsMenuRight.add(btnSearchGroup);

		groupsContainer = new JPanel();
		groupsTab.add(groupsContainer);

		showAllGroups = new JPanel();
		showAllGroups.setLayout(new BorderLayout(0, 0));

		JPanel showAllGroupsList = new JPanel();
		showAllGroupsList.setBorder(new TitledBorder(null, "All Groups",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		showAllGroups.add(showAllGroupsList, BorderLayout.CENTER);
		showAllGroupsList.setLayout(new BoxLayout(showAllGroupsList,
				BoxLayout.X_AXIS));

		table = new JTable(tableModel);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

		table.setAutoCreateRowSorter(true);

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int row = table.rowAtPoint(e.getPoint());
				if (e.getClickCount() == 2) {
					showGroup(Integer.valueOf((Integer) tableModel.getValueAt(
							row, 0)));
				}
			}
		});

		JScrollPane scrollPane = new JScrollPane();
		showAllGroupsList.add(scrollPane);

		scrollPane.setViewportView(table);

		groupsContainer.setLayout(new CardLayout(0, 0));
		groupsContainer.add(showAllGroups, "showAllGroups");

		showGroup = new JPanel();
		groupsContainer.add(showGroup, "showGroup");
		showGroup.setLayout(new BorderLayout(0, 0));

		JPanel showGroupInfoContainer = new JPanel();
		showGroupInfoContainer.setBorder(new TitledBorder(null, "Group Info",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		showGroup.add(showGroupInfoContainer, BorderLayout.NORTH);

		JPanel showGroupInfo = new JPanel();
		showGroupInfoContainer.add(showGroupInfo);
		showGroupInfo.setLayout(new BoxLayout(showGroupInfo, BoxLayout.X_AXIS));

		JPanel showGroupInfoLeft = new JPanel();
		showGroupInfo.add(showGroupInfoLeft);
		showGroupInfoLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel showIDLabelPanel = new JPanel();
		showGroupInfoLeft.add(showIDLabelPanel);
		showIDLabelPanel.setLayout(new BoxLayout(showIDLabelPanel,
				BoxLayout.X_AXIS));

		JLabel showIDLabel = new JLabel("ID:");
		showIDLabelPanel.add(showIDLabel);

		JPanel showPaymentTermLabelPanel = new JPanel();
		showGroupInfoLeft.add(showPaymentTermLabelPanel);
		showPaymentTermLabelPanel.setLayout(new BoxLayout(
				showPaymentTermLabelPanel, BoxLayout.X_AXIS));

		JLabel showPaymentTermLabel = new JLabel("Payment Term:");
		showPaymentTermLabelPanel.add(showPaymentTermLabel);

		JPanel showCraftmanDiscountLabelPanel = new JPanel();
		showGroupInfoLeft.add(showCraftmanDiscountLabelPanel);
		showCraftmanDiscountLabelPanel.setLayout(new BoxLayout(
				showCraftmanDiscountLabelPanel, BoxLayout.X_AXIS));

		JLabel showCraftmanDiscountLabel = new JLabel("Craftman Discount:");
		showCraftmanDiscountLabelPanel.add(showCraftmanDiscountLabel);

		JPanel showQuantityDiscountLabelPanel = new JPanel();
		showGroupInfoLeft.add(showQuantityDiscountLabelPanel);
		showQuantityDiscountLabelPanel.setLayout(new BoxLayout(
				showQuantityDiscountLabelPanel, BoxLayout.X_AXIS));

		JLabel showQuanityDiscountLabel = new JLabel("Quantity Discount:");
		showQuantityDiscountLabelPanel.add(showQuanityDiscountLabel);

		JPanel showPickupDiscountLabelPanel = new JPanel();
		showGroupInfoLeft.add(showPickupDiscountLabelPanel);
		showPickupDiscountLabelPanel.setLayout(new BoxLayout(
				showPickupDiscountLabelPanel, BoxLayout.X_AXIS));

		JLabel showPickupDiscountLabel = new JLabel("Pickup Discount:");
		showPickupDiscountLabelPanel.add(showPickupDiscountLabel);

		JPanel showGroupInfoRight = new JPanel();
		showGroupInfo.add(showGroupInfoRight);
		showGroupInfoRight.setLayout(new BoxLayout(showGroupInfoRight,
				BoxLayout.Y_AXIS));

		JPanel showIDFieldPanel = new JPanel();
		showGroupInfoRight.add(showIDFieldPanel);

		showIDField = new JTextField();
		showIDField.setEditable(false);
		showIDFieldPanel.add(showIDField);
		showIDField.setColumns(10);

		JPanel showPaymentTermFieldPanel = new JPanel();
		showGroupInfoRight.add(showPaymentTermFieldPanel);

		showPaymentTermField = new JTextField();
		showPaymentTermField.setEditable(false);
		showPaymentTermFieldPanel.add(showPaymentTermField);
		showPaymentTermField.setColumns(10);

		JPanel showCraftmanDiscountFieldPanel = new JPanel();
		showGroupInfoRight.add(showCraftmanDiscountFieldPanel);

		showCraftmanDiscountPanel = new JTextField();
		showCraftmanDiscountPanel.setEditable(false);
		showCraftmanDiscountFieldPanel.add(showCraftmanDiscountPanel);
		showCraftmanDiscountPanel.setColumns(10);

		JPanel showQuantityDiscountFieldPanel = new JPanel();
		showGroupInfoRight.add(showQuantityDiscountFieldPanel);

		showQuantitryDiscountField = new JTextField();
		showQuantitryDiscountField.setEditable(false);
		showQuantityDiscountFieldPanel.add(showQuantitryDiscountField);
		showQuantitryDiscountField.setColumns(10);

		JPanel showPickupDiscountFieldPanel = new JPanel();
		showGroupInfoRight.add(showPickupDiscountFieldPanel);

		showPickupDiscountfield = new JTextField();
		showPickupDiscountfield.setEditable(false);
		showPickupDiscountFieldPanel.add(showPickupDiscountfield);
		showPickupDiscountfield.setColumns(10);

		JPanel showGroupButtons = new JPanel();
		showGroup.add(showGroupButtons, BorderLayout.CENTER);
		showGroupButtons.setLayout(new BorderLayout(0, 0));

		JPanel showGroupButtonsLeft = new JPanel();
		showGroupButtons.add(showGroupButtonsLeft, BorderLayout.CENTER);

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("manager")) {
			JButton btwShowGroupEdit = new JButton("Edit");
			btwShowGroupEdit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					editGroup((int) Integer.parseInt(showIDField.getText()));
				}
			});
			showGroupButtonsLeft.add(btwShowGroupEdit);

			JButton btnShowGroupDelete = new JButton("Delete");
			btnShowGroupDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int reply = JOptionPane.showConfirmDialog(null,
							"Are you sure you want to delete this group ?",
							"Delete Group", JOptionPane.YES_NO_OPTION,
							JOptionPane.WARNING_MESSAGE);
					if (reply == JOptionPane.YES_OPTION) {
						boolean success = true;
						try {
							groupCtr.deleteGroup(Integer.parseInt(showIDField
									.getText()));
						} catch (NumberFormatException e1) {
							success = false;
							JOptionPane.showMessageDialog(null,
									"Field value invalid", "Error",
									JOptionPane.ERROR_MESSAGE);
						} catch (RealException e1) {
							success = false;
							JOptionPane.showMessageDialog(null, e1.getError(),
									"Error", JOptionPane.ERROR_MESSAGE);
						}
						if (success) {
							JOptionPane.showMessageDialog(null,
									"The group was deleted", "Delete Group",
									JOptionPane.INFORMATION_MESSAGE);
							showAllGroups();
						}
					}
				}
			});
			showGroupButtonsLeft.add(btnShowGroupDelete);
		}

		JPanel showGroupButtonsRight = new JPanel();
		showGroupButtons.add(showGroupButtonsRight, BorderLayout.WEST);

		JButton btnShowGroupBack = new JButton("Back");
		btnShowGroupBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showAllGroups();
			}
		});
		showGroupButtonsRight.add(btnShowGroupBack);

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("manager")) {
			createGroup = new JPanel();
			createGroup.setLayout(new BorderLayout(0, 0));

			groupsContainer.add(createGroup, "createGroup");

			JPanel createGroupLayout = new JPanel();
			createGroupLayout.setBorder(new TitledBorder(new EtchedBorder(
					EtchedBorder.LOWERED, null, null), "Group Info",
					TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0,
							0, 0)));
			createGroup.add(createGroupLayout, BorderLayout.NORTH);
			createGroupLayout
					.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

			JPanel createGroupInfo = new JPanel();
			createGroupLayout.add(createGroupInfo);
			createGroupInfo.setLayout(new BoxLayout(createGroupInfo,
					BoxLayout.X_AXIS));

			JPanel createGroupLeft = new JPanel();
			createGroupInfo.add(createGroupLeft);
			createGroupLeft.setLayout(new GridLayout(0, 1, 0, 0));

			JPanel createPaymentTermLabelPanel = new JPanel();
			createGroupLeft.add(createPaymentTermLabelPanel);
			createPaymentTermLabelPanel.setLayout(new BoxLayout(
					createPaymentTermLabelPanel, BoxLayout.X_AXIS));

			JLabel paymentTermLabel = new JLabel("Payment Term:");
			createPaymentTermLabelPanel.add(paymentTermLabel);

			JPanel createCraftmanDiscountLabelPanel = new JPanel();
			createGroupLeft.add(createCraftmanDiscountLabelPanel);
			createCraftmanDiscountLabelPanel.setLayout(new BoxLayout(
					createCraftmanDiscountLabelPanel, BoxLayout.X_AXIS));

			JLabel craftsmanDiscountLabel = new JLabel("Craftman Discount:");
			createCraftmanDiscountLabelPanel.add(craftsmanDiscountLabel);

			JPanel createQuantityDiscountLabelPanel = new JPanel();
			createGroupLeft.add(createQuantityDiscountLabelPanel);
			createQuantityDiscountLabelPanel.setLayout(new BoxLayout(
					createQuantityDiscountLabelPanel, BoxLayout.X_AXIS));

			JLabel quantityDiscountLabel = new JLabel("Quantity Discount:");
			createQuantityDiscountLabelPanel.add(quantityDiscountLabel);

			JPanel createpPickUpDiscountLabelPanel = new JPanel();
			createGroupLeft.add(createpPickUpDiscountLabelPanel);
			createpPickUpDiscountLabelPanel.setLayout(new BoxLayout(
					createpPickUpDiscountLabelPanel, BoxLayout.X_AXIS));

			JLabel pickUpDiscountLabel = new JLabel("Pickup Discount:");
			createpPickUpDiscountLabelPanel.add(pickUpDiscountLabel);

			JPanel createGroupRight = new JPanel();
			createGroupInfo.add(createGroupRight);
			createGroupRight.setLayout(new BoxLayout(createGroupRight,
					BoxLayout.Y_AXIS));

			JPanel createPaymentTermFieldPanel = new JPanel();
			createGroupRight.add(createPaymentTermFieldPanel);

			createPaymentTermField = new JTextField();
			createPaymentTermFieldPanel.add(createPaymentTermField);
			createPaymentTermField.setColumns(10);

			JPanel createCraftmanDiscountFieldPanel = new JPanel();
			createGroupRight.add(createCraftmanDiscountFieldPanel);

			createCraftmanDiscountField = new JTextField();
			createCraftmanDiscountFieldPanel.add(createCraftmanDiscountField);
			createCraftmanDiscountField.setColumns(10);

			JPanel createQuantityDiscountFieldPanel = new JPanel();
			createGroupRight.add(createQuantityDiscountFieldPanel);

			createQuantityDiscountField = new JTextField();
			createQuantityDiscountFieldPanel.add(createQuantityDiscountField);
			createQuantityDiscountField.setColumns(10);

			JPanel createPickupDiscountFieldPanel = new JPanel();
			createGroupRight.add(createPickupDiscountFieldPanel);

			createPickupDiscountField = new JTextField();
			createPickupDiscountFieldPanel.add(createPickupDiscountField);
			createPickupDiscountField.setColumns(10);

			JPanel createGroupButtons = new JPanel();
			createGroup.add(createGroupButtons, BorderLayout.CENTER);

			JButton btnCreateGroupNow = new JButton("Create Group");
			btnCreateGroupNow.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean success = true;
					try {
						groupCtr.createGroup(Integer
								.parseInt(createPaymentTermField.getText()),
								Double.parseDouble(createCraftmanDiscountField
										.getText()),
								Double.parseDouble(createQuantityDiscountField
										.getText()), Double
										.parseDouble(createPickupDiscountField
												.getText()));
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						JOptionPane.showMessageDialog(null,
								"The group was created", "Create Group",
								JOptionPane.INFORMATION_MESSAGE);
						showAllGroups();
					}
				}
			});
			createGroupButtons.add(btnCreateGroupNow);

			JButton btnCancelGrupCreate = new JButton("Cancel");
			btnCancelGrupCreate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CardLayout cl = (CardLayout) groupsContainer.getLayout();
					cl.show(groupsContainer, "showAllGroups");
				}
			});
			createGroupButtons.add(btnCancelGrupCreate);

			editGroup = new JPanel();
			editGroup.setLayout(new BorderLayout(0, 0));
			groupsContainer.add(editGroup, "editGroup");

			JPanel editGroupLayout = new JPanel();
			editGroupLayout.setBorder(new TitledBorder(null, "Edit Group",
					TitledBorder.LEADING, TitledBorder.TOP, null, null));
			editGroup.add(editGroupLayout, BorderLayout.NORTH);

			JPanel editGroupInfo = new JPanel();
			editGroupLayout.add(editGroupInfo);
			editGroupInfo.setLayout(new BoxLayout(editGroupInfo,
					BoxLayout.X_AXIS));

			JPanel editGroupLeft = new JPanel();
			editGroupInfo.add(editGroupLeft);
			editGroupLeft.setLayout(new GridLayout(0, 1, 0, 0));

			JPanel editGroupIDLabelPanel = new JPanel();
			editGroupLeft.add(editGroupIDLabelPanel);
			editGroupIDLabelPanel.setLayout(new BoxLayout(
					editGroupIDLabelPanel, BoxLayout.X_AXIS));

			JLabel editGroupIDLabel = new JLabel("ID:");
			editGroupIDLabelPanel.add(editGroupIDLabel);

			JPanel editGroupPaymentTermLabelPanel = new JPanel();
			editGroupLeft.add(editGroupPaymentTermLabelPanel);
			editGroupPaymentTermLabelPanel.setLayout(new BoxLayout(
					editGroupPaymentTermLabelPanel, BoxLayout.X_AXIS));

			JLabel editGroupPaymentTermLabel = new JLabel("Payment Term:");
			editGroupPaymentTermLabelPanel.add(editGroupPaymentTermLabel);

			JPanel editGroupCraftmanDiscountLabelPanel = new JPanel();
			editGroupLeft.add(editGroupCraftmanDiscountLabelPanel);
			editGroupCraftmanDiscountLabelPanel.setLayout(new BoxLayout(
					editGroupCraftmanDiscountLabelPanel, BoxLayout.X_AXIS));

			JLabel editGroupCraftmanDiscountLabel = new JLabel(
					"Craftman Discount:");
			editGroupCraftmanDiscountLabelPanel
					.add(editGroupCraftmanDiscountLabel);

			JPanel editGroupQuantityDiscountLabelPanel = new JPanel();
			editGroupLeft.add(editGroupQuantityDiscountLabelPanel);
			editGroupQuantityDiscountLabelPanel.setLayout(new BoxLayout(
					editGroupQuantityDiscountLabelPanel, BoxLayout.X_AXIS));

			JLabel editGroupQuantityDiscount = new JLabel("Quantity Discount:");
			editGroupQuantityDiscountLabelPanel.add(editGroupQuantityDiscount);

			JPanel editPickupDiscountFieldPanel = new JPanel();
			editGroupLeft.add(editPickupDiscountFieldPanel);
			editPickupDiscountFieldPanel.setLayout(new BoxLayout(
					editPickupDiscountFieldPanel, BoxLayout.X_AXIS));

			JLabel editPickupDiscountField = new JLabel("Pickup Discount:");
			editPickupDiscountFieldPanel.add(editPickupDiscountField);

			JPanel editGroupRight = new JPanel();
			editGroupInfo.add(editGroupRight);
			editGroupRight.setLayout(new BoxLayout(editGroupRight,
					BoxLayout.Y_AXIS));

			JPanel editGroupIDFieldPanel = new JPanel();
			editGroupRight.add(editGroupIDFieldPanel);

			editGroupIDField = new JTextField();
			editGroupIDField.setEditable(false);
			editGroupIDFieldPanel.add(editGroupIDField);
			editGroupIDField.setColumns(10);

			JPanel editGroupPaymentTermFieldPanel = new JPanel();
			editGroupRight.add(editGroupPaymentTermFieldPanel);

			editGroupPaymentTermField = new JTextField();
			editGroupPaymentTermFieldPanel.add(editGroupPaymentTermField);
			editGroupPaymentTermField.setColumns(10);

			JPanel editGroupCraftmanDiscountFieldPanel = new JPanel();
			editGroupRight.add(editGroupCraftmanDiscountFieldPanel);

			editGroupCraftmanDiscountField = new JTextField();
			editGroupCraftmanDiscountFieldPanel
					.add(editGroupCraftmanDiscountField);
			editGroupCraftmanDiscountField.setColumns(10);

			JPanel editGroupQuantityDiscountPanel = new JPanel();
			editGroupRight.add(editGroupQuantityDiscountPanel);

			editGroupQuantityDiscountField = new JTextField();
			editGroupQuantityDiscountPanel.add(editGroupQuantityDiscountField);
			editGroupQuantityDiscountField.setColumns(10);

			JPanel editGroupPickupDiscountPanel = new JPanel();
			editGroupRight.add(editGroupPickupDiscountPanel);

			editGroupPickupDiscountField = new JTextField();
			editGroupPickupDiscountPanel.add(editGroupPickupDiscountField);
			editGroupPickupDiscountField.setColumns(10);

			JPanel editGroupButtons = new JPanel();
			editGroup.add(editGroupButtons, BorderLayout.CENTER);

			JButton btnEditGroupNow = new JButton("Edit");
			btnEditGroupNow.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int reply = JOptionPane.showConfirmDialog(null,
							"Are you sure you want to edit this group ?",
							"Edit Group", JOptionPane.YES_NO_OPTION,
							JOptionPane.WARNING_MESSAGE);
					if (reply == JOptionPane.YES_OPTION) {
						boolean success = true;
						try {
							groupCtr.updateGroup(Integer
									.parseInt(editGroupIDField.getText()), 1,
									editGroupPaymentTermField.getText());
							groupCtr.updateGroup(Integer
									.parseInt(editGroupIDField.getText()), 2,
									editGroupCraftmanDiscountField.getText());
							groupCtr.updateGroup(Integer
									.parseInt(editGroupIDField.getText()), 3,
									editGroupQuantityDiscountField.getText());
							groupCtr.updateGroup(Integer
									.parseInt(editGroupIDField.getText()), 4,
									editGroupPickupDiscountField.getText());
						} catch (NumberFormatException e1) {
							success = false;
							JOptionPane.showMessageDialog(null,
									"Field value invalid", "Error",
									JOptionPane.ERROR_MESSAGE);
						} catch (RealException e1) {
							success = false;
							JOptionPane.showMessageDialog(null, e1.getError(),
									"Error", JOptionPane.ERROR_MESSAGE);
						}
						if (success) {
							JOptionPane.showMessageDialog(null,
									"The group was edited", "Edit Group",
									JOptionPane.INFORMATION_MESSAGE);
							showGroup(Integer.parseInt(showIDField.getText()));
						}
					}
				}
			});

			editGroupButtons.add(btnEditGroupNow);

			JButton btnCancelGroupEdit = new JButton("Cancel");
			btnCancelGroupEdit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					showGroup((int) Integer.parseInt(editGroupIDField.getText()));
				}
			});
			editGroupButtons.add(btnCancelGroupEdit);

		}

		showAllGroups();
	}

	protected void showAllGroups() {
		ArrayList<Group> groups = groupCtr.getGroupsAL();
		tableModel.setRowCount(0);
		for (Group group : groups) {
			tableModel.addRow(new Object[] { group.getId(),
					group.getPaymentTerm(), group.getCraftmanDiscount(),
					group.getQuantityDiscount(), group.getPickUpDiscount() });
		}
		CardLayout cl_1 = (CardLayout) (groupsContainer.getLayout());
		cl_1.show(groupsContainer, "showAllGroups");

	}

	protected void showGroup(int id) {
		boolean success = true;
		Group group = null;
		try {
			group = groupCtr.getGroup(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			showIDField.setText(Integer.toString(group.getId()));
			showPaymentTermField.setText(Integer.toString(group
					.getPaymentTerm()));
			showCraftmanDiscountPanel.setText(Double.toString(group
					.getCraftmanDiscount()));
			showQuantitryDiscountField.setText(Double.toString(group
					.getQuantityDiscount()));
			showPickupDiscountfield.setText(Double.toString(group
					.getPickUpDiscount()));

			CardLayout cl = (CardLayout) (groupsContainer.getLayout());
			cl.show(groupsContainer, "showGroup");
		}

	}

	protected void editGroup(int id) {
		Group group = null;
		boolean success = true;
		try {
			group = groupCtr.getGroup(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			editGroupIDField.setText(Integer.toString(group.getId()));
			editGroupPaymentTermField.setText(Integer.toString(group
					.getPaymentTerm()));
			editGroupCraftmanDiscountField.setText(Double.toString(group
					.getCraftmanDiscount()));
			editGroupQuantityDiscountField.setText(Double.toString(group
					.getQuantityDiscount()));
			editGroupPickupDiscountField.setText(Double.toString(group
					.getPickUpDiscount()));

			CardLayout cl = (CardLayout) (groupsContainer.getLayout());
			cl.show(groupsContainer, "editGroup");
		}
	}

}
